using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using HCManage.Api.Models;
using HCManage.Model;
using HCManage.IBll;
using HCManage.Api.Models.Fileter;

namespace HCManage.Api.Controllers.BaseInfo
{
    [Route("api/[controller]")]
    [ApiController]
    public class InterfaceParamController : ControllerBase
    {
        #region 模块
        private readonly ITblInterfaceParamBll interfaceParamBll;

        public DataResultModel dataResultModel = new DataResultModel();

        #endregion

        #region 构造方法
        public InterfaceParamController(ITblInterfaceParamBll interface_param_bll)
        {
            this.interfaceParamBll = interface_param_bll;
        }
        #endregion

        #region 查询
        /// <summary>
        /// 根据键值获取一条接口参数信息
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        [ResultExceptionFilter]
        public JsonResult Get(int id)
        {
            tbl_interface_param model = interfaceParamBll.Get(id);
            dataResultModel.success = true;
            dataResultModel.data = model;
            return new JsonResult(dataResultModel);
        }


        /// <summary>
        /// 查询接口参数信息
        /// </summary>
        /// <param name="param_name">参数名称</param>
        /// <param name="page">页码</param>
        /// <param name="page_size">显示条数</param>
        /// <returns></returns>
        [HttpGet]
        [ResultExceptionFilter]
        public JsonResult List(string param_name, int? page = 1, int? page_size = 30)
        {
            Tuple<List<tbl_interface_param>, int> result = interfaceParamBll.List(param_name, page.Value, page_size.Value);
            dataResultModel.success = true;
            dataResultModel.data = result.Item1;
            dataResultModel.total = result.Item2;
            return new JsonResult(dataResultModel);
        }

        #endregion

        #region 提交
        /// <summary>
        /// 添加接口参数信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [ResultExceptionFilter]
        public JsonResult Post(tbl_interface_param model)
        {
            if (model == null)
            {
                throw new Exception("请传入参数！");
            }
            bool result = interfaceParamBll.Insert(model);
            if (!result)
            {
                throw new Exception("新增失败！");
            }
            dataResultModel.success = true;
            return new JsonResult(dataResultModel);
        }

        /// <summary>
        /// 修改接口参数信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut]
        [ResultExceptionFilter]
        public JsonResult Put(tbl_interface_param model)
        {
            if (model == null)
            {
                throw new Exception("请传入参数！");
            }
            bool result = interfaceParamBll.Update(model);
            if (!result)
            {
                throw new Exception("修改失败！");
            }
            dataResultModel.success = true;
            return new JsonResult(dataResultModel);
        }

        /// <summary>
        /// 删除接口参数信息
        /// </summary>
        /// <param name="interface_param_id"></param>
        /// <returns></returns>
        [HttpDelete]
        [ResultExceptionFilter]
        public JsonResult Delete(string id)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                throw new Exception("请传入参数！");
            }
            bool result = interfaceParamBll.Delete(id);
            if (!result)
            {
                throw new Exception("删除失败！");
            }
            dataResultModel.success = true;
            return new JsonResult(dataResultModel);
        }
        #endregion
    }
}
